import { request } from 'ice';
import { Pages, PagingInfo } from '@/services/images';

export interface defaultType<T> {
  success: boolean;
  message: string;
  data?: T;
  code?: string;
  status?: number;
}

// 基础用户接口，包含公共属性
interface BaseUser {
  id: number;
  name: string;
  email: string;
  createdAt: string;
}

// 用户类型接口，status 是对象
export interface userType extends BaseUser {
  status: {
    id: number;
    name: string;
  };
}

// 更新用户类型接口，status 是数值
export interface updateUserType extends BaseUser {
  status?: number;
  password?: string;
}


export default {
  // 登录
  async login(email: string, password: string) {
    return request<defaultType<{
      token: string;
      user: userType;
    }>>({
      url: '/user/login',
      method: 'POST',
      data: {
        email,
        password,
      },
    });
  },
  // 注册 普通用户
  async register(name: string, email: string, password: string) {
    return await request<defaultType<boolean>>(
      {
        url: '/user/register',
        method: 'POST',
        data: {
          name,
          email,
          password,
        },
      },
    );
  },

  // 管理员添加用户
  async addUser(name: string, email: string, password: string, status: number) {
    return await request<defaultType<boolean>>(
      {
        url: '/user/addUser',
        method: 'POST',
        data: {
          name,
          email,
          password,
          status,
        },
        headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
      },
    );
  },

  // 管理员修改用户
  async updateUser(id: number, name: string, status: number) {
    return await request<defaultType<boolean>>({
      url: '/user/updateUser',
      method: 'POST',
      data: {
        id,
        name,
        status,
      },
      headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
    });
  },
  // 修改个人信息
  async updateMy(data: updateUserType) {
    return await request<defaultType<boolean>>({
      url: '/user/updateMy',
      method: 'PUT',
      data: data,
      headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
    });
  },

  // 管理员删除用户
  async deleteUser(id: number) {
    return await request<defaultType<boolean>>({
      url: '/user/del',
      method: 'DELETE',
      data: {
        id,
      },
      headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
    });
  },


  // 根据 token 获取用户信息
  async info() {
    return await request<defaultType<userType>>({
      url: '/user/info',
      method: 'GET',
      headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
    });
  },

  // 获取用户列表 管理员权限
  async userList(pagingInfo?: PagingInfo, user?: userType) {
    if (pagingInfo?.skip) {
      pagingInfo.skip = pagingInfo.skip - 1;
    }
    return await request<defaultType<Pages<userType[]>>>({
      url: '/user/list',
      params: {
        ...pagingInfo,
        user: user,
      },
      headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
    });
  },
};
